package com.arthur.controller;

import com.arthur.bean.Cart;
import com.arthur.bean.Order;
import com.arthur.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
public class CartToOrderController {
    @Autowired
    @Qualifier("userService")
    private UserService userService;
    @RequestMapping("/CartToOrderController")
    @ResponseBody
    protected Map cartToOrder(HttpServletRequest request, HttpServletResponse response)throws Exception{
        Map<String, Object> objectMap = new HashMap<>();
        response.setCharacterEncoding("UTF-8");
        String ocid=request.getParameter("ocid");
        String[] ocids=ocid.split(" ");
        for(String oocid:ocids){
            Cart cartinfoList=userService.fetchCartById(oocid);
            String username=cartinfoList.getUsername();
            String productname=cartinfoList.getProductname();
            int orderid=(int)((Math.random()*9+1)*100000);
            String isout="未发货";
            String riqi=request.getParameter("date");

            Order order=new Order();
            order.setOrderid(orderid);
            order.setPname(productname);
            order.setUname(username);
            order.setIsout(isout);
            order.setRiqi(riqi);

            int count = userService.addOrderinfo(order);
            int ioocid=Integer.parseInt(oocid);
            int rows = userService.deleteCartinfoById(ioocid);
            if (count==1&&rows==1){
                objectMap.put("result","1");
            }
        }
        return objectMap;
    }
}
